﻿namespace QLPhongKham.BaseClass.VienPhi
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtovpThanhToanVP
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deid;
        private decimal _deiddangnhap;
        private long _deidql;
        private decimal _desophieu;
        private decimal _dethucthu;
        private decimal _detongtien;
        private int _ihoantra;
        private int _iidgioitinh;
        private int _iidquyenso;
        private int _ilanin;
        private string _sdiachi;
        private string _sghichu;
        private string _shoadon;
        private string _shoten;
        private string _snamsinh;
        private string _sngaycapnhat;
        private string _sngaythu;
        private string _sSchema;
        private int _ssobienlai;
        private string _ssoluutru;
        private string _stenmaytinh;

        public dtovpThanhToanVP()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtovpThanhToanVP(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtovpThanhToanVP.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, "=", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtovpThanhToanVP.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtovpThanhToanVP.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtovpThanhToanVP.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtovpThanhToanVP.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDQL, ",", columnHOADON, ",", columnSOLUUTRU, ",", columnSOBIENLAI, ",", columnTONGTIEN, 
                    ",", columnTHUCTHU, ",", columnNGAYTHU, ",", columnGHICHU, ",", columnTENMAYTINH, ",", columnHOANTRA, ",", columnLANIN, ",", columnSOPHIEU, ",", columnHOTEN, 
                    ",", columnIDGIOITINH, ",", columnDIACHI, ",", columnIDDANGNHAP, ",", columnNAMSINH, ",", columnIDQUYENSO, ") values(", this._deid, ",", this._deidql, ",'", this._shoadon, 
                    "','", this._ssoluutru, "',", this._ssobienlai, ",:", columnTONGTIEN, ",:", columnTHUCTHU, ",to_timestamp('", this._sngaythu, "','dd/mm/yyyy hh24:mi'),:", columnGHICHU, ",:", columnTENMAYTINH, ",", this._ihoantra, 
                    ",", this._ilanin, ",", this._desophieu, ",:", columnHOTEN, ",", this._iidgioitinh, ",:", columnDIACHI, ",", this._deiddangnhap, ",'", this._snamsinh, "',", this._iidquyenso, 
                    ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                command.Parameters.Add(columnHOTEN, NpgsqlDbType.Text, 200).Value = this._shoten;
                command.Parameters.Add(columnTONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._detongtien;
                command.Parameters.Add(columnTHUCTHU, NpgsqlDbType.Numeric, 20).Value = this._dethucthu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtovpThanhToanVP.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidql = 0L;
            this._shoadon = "";
            this._ssoluutru = "";
            this._ssobienlai = 0;
            this._detongtien = 0M;
            this._dethucthu = 0M;
            this._sngaythu = "";
            this._sghichu = "";
            this._stenmaytinh = "";
            this._sngaycapnhat = "";
            this._ihoantra = 0;
            this._ilanin = 0;
            this._desophieu = 0M;
            this._shoten = "";
            this._iidgioitinh = 0;
            this._sdiachi = "";
            this._deiddangnhap = 0M;
            this._snamsinh = "0";
            this._iidquyenso = 0;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtovpThanhToanVP.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDQL, ",", columnHOADON, ",", columnSOLUUTRU, ",", columnSOBIENLAI, ",", columnTONGTIEN, ",", columnTHUCTHU, ",to_char(", columnNGAYTHU, 
                    ",'dd/mm/yyyy hh24:mi') as ", columnNGAYTHU, ",", columnGHICHU, ",", columnTENMAYTINH, ",to_char(", columnNGAYCAPNHAT, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYCAPNHAT, ",", columnHOANTRA, ",", columnLANIN, ",", columnSOPHIEU, 
                    ",", columnHOTEN, ",", columnIDGIOITINH, ",", columnDIACHI, ",", columnIDDANGNHAP, ",", columnNAMSINH, ",", columnIDQUYENSO, " from ", this._sSchema, ".", tableName, 
                    " where ", columnID, " = ", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._deidql = long.Parse(dataTable.Rows[0][columnIDQL].ToString());
                    this._shoadon = dataTable.Rows[0][columnHOADON].ToString();
                    this._ssoluutru = dataTable.Rows[0][columnSOLUUTRU].ToString();
                    this._ssobienlai = int.Parse(dataTable.Rows[0][columnSOBIENLAI].ToString());
                    this._detongtien = decimal.Parse(dataTable.Rows[0][columnTONGTIEN].ToString());
                    this._dethucthu = decimal.Parse(dataTable.Rows[0][columnTHUCTHU].ToString());
                    this._sngaythu = dataTable.Rows[0][columnNGAYTHU].ToString();
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycapnhat = dataTable.Rows[0][columnNGAYCAPNHAT].ToString();
                    this._ihoantra = int.Parse(dataTable.Rows[0][columnHOANTRA].ToString());
                    this._ilanin = int.Parse(dataTable.Rows[0][columnLANIN].ToString());
                    this._desophieu = decimal.Parse(dataTable.Rows[0][columnSOPHIEU].ToString());
                    this._shoten = dataTable.Rows[0][columnHOTEN].ToString();
                    this._iidgioitinh = int.Parse(dataTable.Rows[0][columnIDGIOITINH].ToString());
                    this._sdiachi = dataTable.Rows[0][columnDIACHI].ToString();
                    this._deiddangnhap = decimal.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._snamsinh = dataTable.Rows[0][columnNAMSINH].ToString();
                    this._iidquyenso = int.Parse(dataTable.Rows[0][columnIDQUYENSO].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtovpThanhToanVP.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtovpThanhToanVP.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDQL + "," + columnHOADON + "," + columnSOLUUTRU + "," + columnSOBIENLAI + "," + columnTONGTIEN + "," + columnTHUCTHU + ",to_char(" + columnNGAYTHU + ",'dd/mm/yyyy hh24:mi') as " + columnNGAYTHU + "," + columnGHICHU + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCAPNHAT + ",'dd/mm/yyyy hh24:mi') as " + columnNGAYCAPNHAT + "," + columnHOANTRA + "," + columnLANIN + "," + columnSOPHIEU + "," + columnHOTEN + "," + columnIDGIOITINH + "," + columnDIACHI + "," + columnIDDANGNHAP + "," + columnNAMSINH + "," + columnIDQUYENSO + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtovpThanhToanVP.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtovpThanhToanVP.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDQL, " = ", this._deidql, ",", columnHOADON, " = '", this._shoadon, "',", columnSOLUUTRU, " = '", this._ssoluutru, 
                    "',", columnSOBIENLAI, " = ", this._ssobienlai, ",", columnTONGTIEN, " = :", columnTONGTIEN, ",", columnTHUCTHU, " = :", columnTHUCTHU, ",", columnNGAYTHU, " = to_timestamp('", this._sngaythu, 
                    "','dd/mm/yyyy hh24:mi'),", columnGHICHU, " = :", columnGHICHU, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, ",", columnNGAYCAPNHAT, " = now(),", columnHOANTRA, " = ", this._ihoantra, ",", columnLANIN, 
                    " = ", this._ilanin, ",", columnSOPHIEU, " = ", this._desophieu, ",", columnHOTEN, " = :", columnHOTEN, ",", columnIDGIOITINH, " = ", this._iidgioitinh, ",", columnDIACHI, 
                    " = :", columnDIACHI, ",", columnIDDANGNHAP, " = ", this._deiddangnhap, ",", columnNAMSINH, " = '", this._snamsinh, "' ,", columnIDQUYENSO, "=", this._iidquyenso, " where ", columnID, 
                    "=", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                command.Parameters.Add(columnHOTEN, NpgsqlDbType.Text, 200).Value = this._shoten;
                command.Parameters.Add(columnTONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._detongtien;
                command.Parameters.Add(columnTHUCTHU, NpgsqlDbType.Numeric, 20).Value = this._dethucthu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtovpThanhToanVP.cs");
                return false;
            }
        }

        public static string columnDIACHI
        {
            get
            {
                return "diachi";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnHOADON
        {
            get
            {
                return "hoadon";
            }
        }

        public static string columnHOANTRA
        {
            get
            {
                return "hoantra";
            }
        }

        public static string columnHOTEN
        {
            get
            {
                return "hoten";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDGIOITINH
        {
            get
            {
                return "idgioitinh";
            }
        }

        public static string columnIDQL
        {
            get
            {
                return "idql";
            }
        }

        public static string columnIDQUYENSO
        {
            get
            {
                return "idquyenso";
            }
        }

        public static string columnLANIN
        {
            get
            {
                return "lanin";
            }
        }

        public static string columnNAMSINH
        {
            get
            {
                return "namsinh";
            }
        }

        public static string columnNGAYCAPNHAT
        {
            get
            {
                return "ngaycapnhat";
            }
        }

        public static string columnNGAYTHU
        {
            get
            {
                return "ngaythu";
            }
        }

        public static string columnSOBIENLAI
        {
            get
            {
                return "sobienlai";
            }
        }

        public static string columnSOLUUTRU
        {
            get
            {
                return "soluutru";
            }
        }

        public static string columnSOPHIEU
        {
            get
            {
                return "sophieu";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public static string columnTHUCTHU
        {
            get
            {
                return "thucthu";
            }
        }

        public static string columnTONGTIEN
        {
            get
            {
                return "tongtien";
            }
        }

        public string pDIACHI
        {
            get
            {
                return this._sdiachi;
            }
            set
            {
                this._sdiachi = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public string pHOADON
        {
            get
            {
                return this._shoadon;
            }
            set
            {
                this._shoadon = value;
            }
        }

        public int pHOANTRA
        {
            get
            {
                return this._ihoantra;
            }
            set
            {
                this._ihoantra = value;
            }
        }

        public string pHOTEN
        {
            get
            {
                return this._shoten;
            }
            set
            {
                this._shoten = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public decimal pIDDANGNHAP
        {
            get
            {
                return this._deiddangnhap;
            }
            set
            {
                this._deiddangnhap = value;
            }
        }

        public int pIDGIOITINH
        {
            get
            {
                return this._iidgioitinh;
            }
            set
            {
                this._iidgioitinh = value;
            }
        }

        public long pIDQL
        {
            get
            {
                return this._deidql;
            }
            set
            {
                this._deidql = value;
            }
        }

        public int pIDQUYENSO
        {
            get
            {
                return this._iidquyenso;
            }
            set
            {
                this._iidquyenso = value;
            }
        }

        public int pLANIN
        {
            get
            {
                return this._ilanin;
            }
            set
            {
                this._ilanin = value;
            }
        }

        public string pNAMSINH
        {
            get
            {
                return this._snamsinh;
            }
            set
            {
                this._snamsinh = value;
            }
        }

        public string pNGAYCAPNHAT
        {
            get
            {
                return this._sngaycapnhat;
            }
            set
            {
                this._sngaycapnhat = value;
            }
        }

        public string pNGAYTHU
        {
            get
            {
                return this._sngaythu;
            }
            set
            {
                this._sngaythu = value;
            }
        }

        public int pSOBIENLAI
        {
            get
            {
                return this._ssobienlai;
            }
            set
            {
                this._ssobienlai = value;
            }
        }

        public string pSOLUUTRU
        {
            get
            {
                return this._ssoluutru;
            }
            set
            {
                this._ssoluutru = value;
            }
        }

        public decimal pSOPHIEU
        {
            get
            {
                return this._desophieu;
            }
            set
            {
                this._desophieu = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public decimal pTHUCTHU
        {
            get
            {
                return this._dethucthu;
            }
            set
            {
                this._dethucthu = value;
            }
        }

        public decimal pTONGTIEN
        {
            get
            {
                return this._detongtien;
            }
            set
            {
                this._detongtien = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "vp_ttvp";
            }
        }
    }
}

